﻿using HuiZe.education.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_Resource_LessionItem.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Resource_LessionItem : UserControl
    {
        public respResourcekinditemModel Data
        {
            get { return (respResourcekinditemModel)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(respResourcekinditemModel), typeof(PART_Resource_LessionItem), new PropertyMetadata(null, (sender, e) =>
            {
                PART_Resource_LessionItem _item = (PART_Resource_LessionItem)sender;
                respResourcekinditemModel _data = null;
                if (e.NewValue != null)
                    _data = (respResourcekinditemModel)e.NewValue;

                _item.Load(_data);
            }));

        public Func<int, bool> CollectCallback { get; set; }

        public Action<respResourcekinditemModel> OpenResourceCallback { get; set; }

        public PART_Resource_LessionItem()
        {
            InitializeComponent();
            _ShowStoryboard = (Storyboard)FindResource("ShowStoryboard");
        }

        public void Load(respResourcekinditemModel _data)
        {
            if (_data == null) return;

            BitmapImage _bmp = new BitmapImage();
            _bmp.CacheOption = BitmapCacheOption.OnLoad;
            _bmp.BeginInit();
            _bmp.UriSource = new Uri(_data.image, UriKind.RelativeOrAbsolute);
            _bmp.EndInit();

            PART_Image.Source = _bmp;
            PART_Title.Text = _data.kind_item_name;
            PART_IsCollected.IsChecked = new bool?(_data.collection == 1);
            PART_Count_View.Text = _data.shownum.ToString();
            PART_Count_Collected.Text = _data.collectionnum.ToString();
            PART_IsCollected.Checked += PART_IsCollected_Checked;
            PART_IsCollected.Unchecked += PART_IsCollected_Unchecked;
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
            {
                _ShowStoryboard.Begin();
            }));
        }

        void PART_IsCollected_Unchecked(object sender, RoutedEventArgs e)
        {
            if (CollectCallback != null && Data != null)
            {
                if (CollectCallback(Data.id))
                {
                    Data.collectionnum--;
                    PART_Count_Collected.Text = Data.collectionnum.ToString();
                }
            }
        }

        void PART_IsCollected_Checked(object sender, RoutedEventArgs e)
        {
            if (CollectCallback != null && Data != null)
            {
                if (CollectCallback(Data.id))
                {
                    Data.collectionnum++;
                    PART_Count_Collected.Text = Data.collectionnum.ToString();
                }
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (OpenResourceCallback != null)
            {
                try
                {
                    OpenResourceCallback(this.Data);
                }
                catch (Exception ex)
                {
                    App.Log(ex);
                }
                e.Handled = true;
            }
            base.OnMouseLeftButtonUp(e);
        }


        Storyboard _ShowStoryboard;
    }
}
